/* Copyright (C) 2007 Ryan Capote
 * This file is apart of BlueEngine
 *  See Licsense.txt
 */
#include "log.h"

namespace BlueEngine
{
    namespace Utilities
    {
         Logger::Logger( std::string filename)
         {
              setLog( filename );
         }

         Logger::~Logger()
         {
              if( m_logfile.is_open() ) 
              {
                   m_logfile.close();
              }
         }

         /*
          * Set the log file to write to
          */
         void Logger::setLog(std::string filename)
         {
              m_logfile.open( filename.c_str(), std::ios::trunc );
   
              assert( m_logfile.is_open() );
         }

         /*
          * Write text to the log file with a timestamp
          */
         void Logger::write( const std::string& text )
         {
              assert( m_logfile.is_open() );

              time_t t;
              time( &t );
              tm *date = localtime( &t );

              m_logfile << "[" 
                        << date->tm_hour
                        << ":"
                        << date->tm_min 
                        << ":"
                        << date->tm_sec 
                        << text 
                        << std::endl;

              std::string value = text;

              // Make sure text gets to the log file.
              m_logfile.flush();
         }
    }
}
